package com.iqiyi.pps.epg.core.service.sginal;

import com.iqiyi.pps.epg.core.dao.sginal.SginalServerDataDao;
import com.iqiyi.pps.epg.core.model.sginal.SginalServer;
import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


/**
 * Created by zhang on 2017/4/27.
 */
@Service
@Transactional
public class SginalServerServiceImpl implements SginalServerService {

    private Logger logger = LoggerFactory.getLogger(SginalServerServiceImpl.class);

    @Autowired
    private SginalServerDataDao dao;

    @Override
    public SginalServer get(int id) {
        return dao.get(id);
    }

    @Override
    public int add(SginalServer entity) {
        dao.saveOnly(entity);
        return entity.getId();
    }

    @Override
    public void update(SginalServer entity) {
        dao.save(entity);
    }

    @Override
    public List<SginalServer> find(int fatherId) {
        Criteria c = dao.createCriteria();
        c.add(Restrictions.eq("fatherId", fatherId));
        c.add(Restrictions.ne("isDelete", SginalServer.IS_DELETE));
        c.addOrder(Order.asc("sequence"));
        return dao.find(c);
    }

    @Override
    public void removeAll(int fatherId) {
        String hql = "update SginalServer set isDelete=1 where fatherId=? and idDelete!=" + SginalServer.IS_DELETE;
        dao.createQuery(hql)
                .setParameter(0, fatherId)
                .executeUpdate();
        return;
    }
}
